$(function() {
//map_div_id, point_position

    initializeMultiplesPointMaps("#empresas-map-canvas", empresas, false);
    categoria_id = $("#Entidad_categoria_id");
    categoria_id.select2({
        placeholder: "Seleccione una Categoria.",
        initSelection: function(element, callback) {
            if ($(element).val()) {
                var data = {id: element.val(), text: $(element).attr('selected-text')};
                callback(data);
            }
        },
        ajax: {// instead of writing the function to execute the request we use Select2's convenient helper
            url: baseUrl + "site/ajaxListSelect2CategoriaIndustria",
            type: "get",
            dataType: 'json',
            data: function(term, page) {
                return {
                    search_value: term
                };
            },
            results: function(data, page) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                return {results: data};
            }
        },
        allowClear: true
    });

    industria_id = categoria_id.val() ? categoria_id.val() : null;
    empresa_id = $("#Entidad_id");
    empresa_id.select2({
        placeholder: "Seleccione una Empresa.",
        initSelection: function(element, callback) {
            if ($(element).val()) {
                var data = {id: element.val(), text: $(element).attr('selected-text')};
                callback(data);
            }
        },
        ajax: {// instead of writing the function to execute the request we use Select2's convenient helper
            url: baseUrl + "site/ajaxListSelect2EmpresasRegistradas",
            type: "get",
            dataType: 'json',
            data: function(term, page) {
                info_busqueda = {search_value: term, industria_id: industria_id};
                return info_busqueda;
            },
            results: function(data, page) { // parse the results into the format expected by Select2.
                // since we are using custom formatting functions we do not need to alter remote JSON data
                return {results: data};
            }
        },
        allowClear: true
    });
    empresa_id.on('change', function() {
        industria_id = categoria_id.val() ? categoria_id.val() : null;
        if (!empresa_id.val()) {
            initializeMultiplesPointMaps("#empresas-map-canvas", empresas, false);
        }
    });
    $("#btnBuscarEmpresa").on('click', function() {
        if (empresa_id.val() || categoria_id.val()) {
            $data = {id_empresa: empresa_id.val() ? empresa_id.val() : null, industria_id: categoria_id.val() ? categoria_id.val() : null}
            getEmpresasSearchPoints("site/ajaxPointsEmpresas", $data, function(data) {
                initializeMultiplesPointMaps("#empresas-map-canvas", data, false);
            });

        }
        else {
            alert("Seleccione Por lo menos uno.");
        }
    });
    categoria_id.on('change', function() {
        industria_id = categoria_id.val() ? categoria_id.val() : null;
        if (!categoria_id.val()) {
            initializeMultiplesPointMaps("#empresas-map-canvas", empresas, false);
        }
    });
});
function getEmpresasSearchPoints(url, $data, callback)
{
    $.ajax({
        type: "GET",
        url: baseUrl + url,
        dataType: 'json',
        data: $data,
        success: function(data) {
            $empresas_points = data;
            callback($empresas_points);
        }
    });

}
function getInformacionDetallada(id) {
    url = baseUrl + "site/Informacion/id/" + id;
    window.open(url, '_blank');
}



